//
//  TransferViewController.m
//  ICrEAM
//
//  Created by Leona on 2017/6/7.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "TransferViewController.h"
#import "TransferTableViewCell.h"
#import "TmOwnerInfoModel.h"

static NSString *cellIdentifer = @"TransferTableViewCell";
@interface TransferViewController ()<UITableViewDelegate,UITableViewDataSource>{
    TmOwnerInfoModel *confirmOwnerInfoModel;
}
@property (nonatomic, strong) TPKeyboardAvoidingScrollView *bigScrollView;
@property (weak, nonatomic) IBOutlet UIButton *offButton;
@property (strong, nonatomic)NSMutableArray *dataArray;

@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel1;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel2;
@property (weak, nonatomic) IBOutlet UIButton *seachButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;


@end

@implementation TransferViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.offButton.layer.masksToBounds = YES;
    self.offButton.layer.cornerRadius = self.offButton.frame.size.height/2;
    //添加的是背景view，并不是自身的view
//    [self.blankView addSubview:self.view];
    self.dataArray = [NSMutableArray array];
    [self.transferTabelView registerNib:[UINib nibWithNibName:@"TransferTableViewCell" bundle:nil] forCellReuseIdentifier:@"TransferTableViewCell"];

    self.confirmButton.alpha = 0.5;
    self.confirmButton.enabled = NO;
    
    self.titleLabel1.textColor = TitleColor;
    self.titleLabel2.textColor = TitleColor;
    [self.seachButton setBackgroundColor:TitleColor];
    [self.confirmButton setBackgroundColor:TitleColor];
    [self.cancelButton setBackgroundColor:TitleColor];

    
    
    
    
    
}
//请求
- (void)getOwnerByNameOrMobile:(NSString *)Owner orMobile:(NSString *)Mobile{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:Owner forKey:@"OWNER_NAME"];
    [parameters setObject:Mobile forKey:@"OWNER_MOBILE"];
    
    [manager startGetRequestWithURL:QueryOwnerByNameOrMobile parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            if (IsArr(data[@"TM_OWNER_INFO"])){
                NSArray*array = data[@"TM_OWNER_INFO"];
                
                if ([array isKindOfClass:[NSArray class]] && array.count > 0) {
                    self.dataArray = [TmOwnerInfoModel mj_objectArrayWithKeyValuesArray:array];
                    
                }else if (array.count == 0){
                    [self.view showString:@"无相同车主" forSeconds:1.5];
//                    [self carChangeAlertMessage:@"无相同车主，是否保存该接车单？" withSuccessBlock:^{
//                    [self hideAllView];
//                    [[NSNotificationCenter defaultCenter] postNotificationName:@"saveOrder"object:nil];
//                        
//                    } failBlock:^{
//                    
//                    
//                    
//                    }];
               
                
                }

            }
            [self.transferTabelView reloadData];
        }
    }];



}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TransferTableViewCell *cell = (TransferTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifer forIndexPath:indexPath];
    cell.listModel = self.dataArray[indexPath.row];
    cell.stateImageView.hidden = YES;
        


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TmOwnerInfoModel *model = [TmOwnerInfoModel mj_objectWithKeyValues:self.dataArray[indexPath.row]];
    self.confirmButton.alpha = 1;
    self.confirmButton.enabled = YES;
    confirmOwnerInfoModel = model;
    if([model.VEHILCE_NUM integerValue]>0){
        
        [self carChangeAlertMessage:@"当前车主名下的其他车辆是否发生过户？" withSuccessBlock:^{
            [self alertMessage:@"请在确认委托书信息时，及时到iCrEAM系统完成车辆的信息变更" withBlock:^{
                
            
            }];

        } failBlock:^{
            
            
            
        }];
        
        
        
    }


    
}


- (IBAction)searchAct:(UIButton *)sender {
    
    [self getOwnerByNameOrMobile:self.ownerTextF.text orMobile:self.ownerPhoneTextF.text];
 
}

- (IBAction)confirmAct:(UIButton *)sender {
    
    if(![confirmOwnerInfoModel.OWNER_NAME isEqualToString:self.ownerTextF.text]||![confirmOwnerInfoModel.MOBILE isEqualToString:self.ownerPhoneTextF.text]){
        [self.view showString:@"接车单中的车主与车主手机与档案不一致，请修正一致后再保存接车单。" forSeconds:1.5];
    }else{
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:confirmOwnerInfoModel.OWNER_NO forKey:@"OWNER_NO"];
        [dic setObject:confirmOwnerInfoModel.MOBILE forKey:@"OWNER_MOBILE"];
        [dic setObject:confirmOwnerInfoModel.OWNER_NAME forKey:@"OWNER_NAME"];
        NSLog(@"dic==%@",dic);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"OWNER_NO" object:nil userInfo:dic];
        
        [self hideAllView];
    }

    
}
- (IBAction)cancelAct:(UIButton *)sender {
    [self hideAllView];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"saveOrder"object:nil];
}

- (UIView *)blankView{
    if (!_blankView) {
        _blankView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _blankView.backgroundColor = [UIColor colorWithHex:BorderColor_Gray_Hex andAlpha:0.8];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAllView)];
        [_blankView addGestureRecognizer:tap];
        
    }
    return _blankView;
}
-(void)hideAllView{
    [self.blankView removeFromSuperview];
    [self.view removeFromSuperview];
}
- (IBAction)offAct:(UIButton *)sender {
    [self hideAllView];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"saveOrder" object:nil];

}
@end
